package coolq.yaozhenyong.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.forte.qqrobot.component.forlemoc.LemocApp;
import com.forte.qqrobot.component.forlemoc.LemocApplication;
import com.forte.qqrobot.component.forlemoc.LinkConfiguration;
import com.forte.qqrobot.sender.MsgSender;
import com.forte.qqrobot.utils.CQCodeUtil;

import coolq.yaozhenyong.bean.QqRobotSet;
import coolq.yaozhenyong.custom.SpringContextUtil;
import coolq.yaozhenyong.service.QqRobotSetService;

public class MyqqRobot implements LemocApp{
	
	/****
	 * 成功以后回调函数
	 */
	@Override
	public void after(CQCodeUtil arg0, MsgSender arg1) {
		
	}
	/** 配置工作 **/
	@Override
	public void before(LinkConfiguration link) {
		//直接在数据库取出配置项目
		QqRobotSetService bean = SpringContextUtil.getBean(QqRobotSetService.class);
		QqRobotSet entity = bean.getById(1);
		if(entity!=null) {
			if(entity.getLocalqqnick()!=null&&entity.getLocalqqnick().length()>1) {
				link.setLocalQQNick(entity.getLocalqqnick());
				systemout("Nick",entity.getLocalqqnick());
			}
			if(entity.getLocalqqcode()!=null&&entity.getLocalqqcode().length()>1) {
				link.setLocalQQCode(entity.getLocalqqcode());
				systemout("Code",entity.getLocalqqcode());
			}
			if(entity.getCqPath()!=null&&entity.getCqPath().length()>1) {
				link.setCqPath(entity.getCqPath());
				systemout("CqPath",entity.getCqPath());
			}
			if(entity.getEncode()!=null&&entity.getEncode().length()>1) {
				link.setEncode(entity.getEncode());
				systemout("Encode",entity.getEncode());
			}
			if(entity.getLinkIp()!=null&&entity.getLinkIp().length()>1) {
				link.setLinkIp(entity.getLinkIp());
				systemout("LinkIp",entity.getLinkIp());
			}
			//设置重连次数
			link.setRetryTime(6);
		}
	}

	public void systemout(String code,String value) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		System.out.println(df.format(new Date())+" QQRobot  配置属性[ "+code+" ]配置内容[ "+value+" ]");
	}
}
